﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using Model;
using System.Data;
using System.Data.SqlClient;
namespace Manager
{
    /// <summary>
    /// HW_School_User业务逻辑
    /// 创建人：Wilson
    /// 创建时间：2013-1-29  
    /// </summary>
    public class HW_School_UserManager
    {
        			
        #region##通过主键ID得到HW_School_User
        /// <summary>
        /// 功能：通过ID得到HW_School_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_School_User GetById(string xx_username)
        {            
            using (HW_School_UserService ws = new HW_School_UserService())
			{
				var user= ws.GetById(xx_username);
                HW_SchoolService service = new HW_SchoolService();
                user.hw_School = service.GetById(user.SchoollCode);
                return user;
			}
        }
        #endregion
		
		#region##通过主键ID删除HW_School_User
        /// <summary>
        /// 功能：通过主键ID删除HW_School_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string xx_username)
		{
			using (HW_School_UserService ws = new HW_School_UserService())
			{
                SqlConnection conn = new SqlConnection(Utility.WebConn.connString);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlTransaction trans = conn.BeginTransaction();
                HW_XXX_UserService xxxuserService = new HW_XXX_UserService();
                try
                {
                    int n = ws.DeleteById(trans, xx_username);
                    xxxuserService.DeleteById(trans, xx_username);
                    
                    trans.Commit();
                    return n;
                }
                catch(Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
			}
		}
        public int ResetPassWord(string xx_username, string pass)
        {
            using (SqlConnection conn = new SqlConnection(Utility.WebConn.connString))
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    HW_School_UserService ws = new HW_School_UserService();
                    HW_XXX_UserService service = new HW_XXX_UserService();
                    service.ResetPassword(trans, xx_username, pass);
                    //string pass=System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    int n = ws.ResetPassword(trans, xx_username, pass);
                    trans.Commit();
                    return n;

                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        public IList<HW_School_User> GetBySchoolCode(string schoolCode)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
            {
                return ws.GetBySchoolCode(schoolCode);
            }
        }
        public string GetMaxUser(string schoolcode)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
            {
                return ws.GetMaxUser(schoolcode);
            }
        }
		#endregion			
		
        #region##添加HW_School_User
        /// <summary>
        /// 功能：添加HW_School_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public void Add(HW_School_User hw_school_user)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
			{
                hw_school_user.RoleId = 3000;
                SqlConnection conn = new SqlConnection(Utility.WebConn.connString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                HW_XXX_UserService manager = new HW_XXX_UserService();
                HW_XXX_User user=new HW_XXX_User ()
                {
                    XX_UserName=hw_school_user.XX_UserName,
                    XX_Pass=hw_school_user.XX_Pass,
                    RoleId=3000,
                    CNName=hw_school_user.CNName,
                };
                try
                {
                    manager.Add(trans,user);
                    ws.Add(trans,hw_school_user);
                    trans.Commit();
                }
                catch(Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
			}
        }
        #endregion
		
		#region##得到HW_School_User集合
		/// <summary>
        /// 功能：得到HW_School_User集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_School_User> GetList()
		{
		    using (HW_School_UserService ws = new HW_School_UserService())
			{
				return ws.GetList();
			}
		}
		#endregion
		
		#region##修改HW_School_User
        /// <summary>
        /// 功能：修改HW_School_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_School_User(HW_School_User hw_school_user)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
			{
				return ws.ModHW_School_User(hw_school_user);
			}
        }
        #endregion
		
		#region##统计HW_School_User
		/// <summary>
        /// 修改HW_School_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			using (HW_School_UserService ws = new HW_School_UserService())
			{
				return ws.SelectCount();
			}
		}
        /// <summary>
        /// 修改HW_School_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            using (HW_School_UserService ws = new HW_School_UserService())
			{
				return ws.SelectCountById();
			}
        }
		#endregion
        #region##自定义HW_School_User
        public DataTable GetSchoolUser(int pageIndex, int pageSize, out int count, string districtCode)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
            {
                return ws.GetSchoolUser(pageIndex, pageSize, out count, districtCode);
            }
        }
        public DataTable Search(int pageIndex, int pageSize, out int count,string districtCode, string searchString)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
            {
                return ws.Search(pageIndex, pageSize, out count,districtCode,searchString);
            }
        }
        public HW_School_User GetByUserName(string userName)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
            {
                HW_SchoolService schoolService=new HW_SchoolService ();
                var schooluser= ws.GetByUserName(userName);
                schooluser.hw_School = schoolService.GetById(schooluser.SchoollCode);
                return schooluser;
            }
        }
        public DataTable GetByOrgCode(int pageIndex,int pageSize,out int count, string orgCode)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
            {
                return ws.GetByOrgCode(pageIndex,pageSize,out count,orgCode);
            }
        }
        public DataTable GetBySchoolCode(int pageIndex, int pageSize, out int count, string schoolCode)
        {
            using (HW_School_UserService ws = new HW_School_UserService())
            {
                return ws.GetBySchoolCode(pageIndex, pageSize, out count, schoolCode);
            }
        }
        #endregion
    }
}